package com.j180.service.supGoods.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.j180.bean.PageBean;
import com.j180.bean.SupGoods;
import com.j180.bean.SupGoodsCount;
import com.j180.dao.supGoods.ISupGoodsDao;
import com.j180.service.supGoods.ISupGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
@Service
public class SupGoodsService implements ISupGoodsService {
    @Autowired
    private ISupGoodsDao iSupGoodsDao;

    @Override
    public PageBean<SupGoods> showSupGoodsBySupplierId(int id, int curpage, int pageSize) {
        PageHelper.startPage(curpage,pageSize);
        List<SupGoods> list = iSupGoodsDao.getSupGoodsBySupplierId(id);
        PageInfo<SupGoods> pageInfo = new PageInfo<>(list);

        PageBean<SupGoods> pageBean = new PageBean<>();
        pageBean.setDatas(list);
        pageBean.setCurrentPage(curpage);
        pageBean.setPageSize(pageSize);
        pageBean.setTolNum((int) pageInfo.getTotal());
        pageBean.setTolPage(pageInfo.getPages());

        return pageBean;
    }

    @Override
    public SupGoods showSupGoodsByIdAndSupplierId(int suppId, int supId) {


        return iSupGoodsDao.getSupGoodsByIdAndSupplierId(suppId,supId);
    }

    @Override
    public int addSupGoodsInfo(SupGoods supGoods) {
        return iSupGoodsDao.insertSupGoods(supGoods);
    }

    @Override
    public int deleteSupGoodsByIdInfo(int id) {
        return iSupGoodsDao.delSupGoodsById(id);
    }

    @Override
    public int updateSupGoodsInfo(SupGoods supGoods) {
        return iSupGoodsDao.upSupGoods(supGoods);
    }

    @Override
    public List<SupGoodsCount> getSupGoodsInfoBySupplierId(@PathVariable("id") int id) {
        List<SupGoodsCount> supGoods = iSupGoodsDao.getSupGoodsInfoBySupplierId(id);
        return  supGoods;
    }

    @Override
    public PageBean<SupGoods> getAllSupGoodsInfo(int curPage, int pageSize) {
        PageHelper.startPage(curPage, pageSize);
        List<SupGoods> supGoods = iSupGoodsDao.getAllSupGoods();
        PageInfo<SupGoods> supPageInfo = new PageInfo<>(supGoods);

        PageBean<SupGoods> pageBean = new PageBean<>();
        pageBean.setCurrentPage(curPage);
        pageBean.setPageSize(pageSize);
        pageBean.setDatas(supGoods);
        pageBean.setTolNum((int) supPageInfo.getTotal());
        pageBean.setTolPage(supPageInfo.getPages());
        return pageBean;
    }
}
